package cn.usersmsg.listener;

import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import cn.usersmsg.model.User;

/**
 * Application Lifecycle Listener implementation class alllistener
 *
 */
@WebListener
public class alllistener implements ServletContextListener, ServletContextAttributeListener, HttpSessionListener, HttpSessionAttributeListener, HttpSessionActivationListener, HttpSessionBindingListener, ServletRequestListener, ServletRequestAttributeListener {
	ServletContext application;

	/**
     * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
     */
    public void sessionCreated(HttpSessionEvent arg0)  { 
         System.out.println("HttpSessionListener#sessionCreated(HttpSessionEvent)");
         System.out.println(arg0.getSession().getId());
         System.out.println(arg0.getSession().getCreationTime());
         System.out.println(arg0.getSession().getLastAccessedTime());
         System.out.println(arg0.getSession().getMaxInactiveInterval());
         System.out.println(arg0.getSession().isNew());
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
         HttpSession session = arg0.getSession();
         session.setAttribute("sdf", sdf);
    }

	/**
     * @see ServletContextAttributeListener#attributeRemoved(ServletContextAttributeEvent)
     */
    public void attributeRemoved(ServletContextAttributeEvent arg0)  { 
         System.out.println("ServletContextAttributeListener#attributeRemoved(ServletContextAttributeEvent)");
         System.out.println(arg0.getName());
         System.out.println(arg0.getServletContext().getServletContextName());
    }

	/**
     * @see ServletRequestAttributeListener#attributeAdded(ServletRequestAttributeEvent)
     */
    public void attributeAdded(ServletRequestAttributeEvent arg0)  { 
         System.out.println("ServletRequestAttributeListener#attributeAdded(ServletRequestAttributeEvent)");
         System.out.println(arg0.getName()+"添加");
         System.out.println(arg0.getServletRequest().getContentLength());
         System.out.println(arg0.getServletRequest().getLocalAddr());
         System.out.println(arg0.getServletRequest().getLocalName());
         System.out.println(arg0.getServletRequest().getLocalPort());
         System.out.println(arg0.getServletRequest().getServerName());
    }

	/**
     * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
     */
    public void attributeReplaced(HttpSessionBindingEvent arg0)  { 
         System.out.println("HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)");
    }

	/**
     * @see HttpSessionActivationListener#sessionWillPassivate(HttpSessionEvent)
     */
    public void sessionWillPassivate(HttpSessionEvent arg0)  { 
         System.out.println("HttpSessionActivationListener#sessionWillPassivate(HttpSessionEvent)");
    }

	/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent arg0)  { 
         System.out.println("ServletContextListener#contextInitialized(ServletContextEvent)");
         application = arg0.getServletContext();
         //创建集合保存在线用户
         Set<User> onlineUsers = new HashSet<User>();
         Map<User, String> useraddr = new Hashtable<User, String>();
         application.setAttribute("onlineUsers", onlineUsers);
         application.setAttribute("useraddr", useraddr);
    }

	/**
     * @see ServletContextAttributeListener#attributeAdded(ServletContextAttributeEvent)
     */
    public void attributeAdded(ServletContextAttributeEvent arg0)  { 
         System.out.println("ServletContextAttributeListener#attributeAdded(ServletContextAttributeEvent)");
         System.out.println("ServletContex创建"+arg0.getName());
    }

	/**
     * @see ServletRequestListener#requestDestroyed(ServletRequestEvent)
     */
    public void requestDestroyed(ServletRequestEvent arg0)  { 
         System.out.println("ServletRequestListener#requestDestroyed(ServletRequestEvent)");
         System.out.println(arg0.getServletRequest().getScheme());
    }

	/**
     * @see ServletRequestAttributeListener#attributeRemoved(ServletRequestAttributeEvent)
     */
    public void attributeRemoved(ServletRequestAttributeEvent arg0)  { 
         System.out.println("ServletRequestAttributeListener#attributeRemoved(ServletRequestAttributeEvent)");
         System.out.println(arg0.getName()+"移除");
    }

	/**
     * @see HttpSessionBindingListener#valueBound(HttpSessionBindingEvent)
     */
    public void valueBound(HttpSessionBindingEvent arg0)  { 
         System.out.println("HttpSessionBindingListener#valueBound(HttpSessionBindingEvent)");
    }

	/**
     * @see ServletRequestListener#requestInitialized(ServletRequestEvent)
     */
    public void requestInitialized(ServletRequestEvent arg0)  { 
         System.out.println("ServletRequestListener#requestInitialized(ServletRequestEvent)");
         System.out.println(arg0.getServletRequest().getContentType());
    }

	/**
     * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
     */
    public void sessionDestroyed(HttpSessionEvent arg0)  { 
         System.out.println("HttpSessionListener#sessionDestroyed(HttpSessionEvent)");
    }

	/**
     * @see HttpSessionActivationListener#sessionDidActivate(HttpSessionEvent)
     */
    public void sessionDidActivate(HttpSessionEvent arg0)  { 
         System.out.println("HttpSessionActivationListener#sessionDidActivate(HttpSessionEvent)");
         HttpSession session = arg0.getSession();
         //获得注销用户
         User user = (User)session.getAttribute("user");
         Set<User> onlineUsers = (Set<User>)application.getAttribute("onlineUsers");
         onlineUsers.remove(user);
    }

	/**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent arg0)  { 
         System.out.println("ServletContextListener#contextDestroyed(ServletContextEvent)");
    }

	/**
     * @see ServletRequestAttributeListener#attributeReplaced(ServletRequestAttributeEvent)
     */
    public void attributeReplaced(ServletRequestAttributeEvent arg0)  { 
         System.out.println("ServletRequestAttributeListener#attributeReplaced(ServletRequestAttributeEvent)");
         System.out.println(arg0.getName()+"Replaced");
    }

	/**
     * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
     */
    public void attributeAdded(HttpSessionBindingEvent arg0)  { 
         System.out.println("HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)");
         HttpSession session = arg0.getSession();
         //获得登陆用户
         if ("user".equals(arg0.getName())) {
        	 User user = (User)session.getAttribute("user");
             Set<User> onlineUsers = (Set<User>)application.getAttribute("onlineUsers");
             Map<User, String> useraddr = (Map<User, String>)application.getAttribute("useraddr");
             //保存到列表里
             onlineUsers.add(user);
		}
        
    }

	/**
     * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
     */
    public void attributeRemoved(HttpSessionBindingEvent arg0)  { 
         System.out.println("HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)");
         //获得注销用户
         User user = (User)arg0.getValue();
         Set<User> onlineUsers = (Set<User>)application.getAttribute("onlineUsers");
         onlineUsers.remove(user);
    }

	/**
     * @see ServletContextAttributeListener#attributeReplaced(ServletContextAttributeEvent)
     */
    public void attributeReplaced(ServletContextAttributeEvent arg0)  { 
         System.out.println("ServletContextAttributeListener#attributeReplaced(ServletContextAttributeEvent)");
    }

	/**
     * @see HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent)
     */
    public void valueUnbound(HttpSessionBindingEvent arg0)  { 
         System.out.println("HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent)");
    }
	
}
